package com.xyht.sca_s.student_manage_system.modules.rxRegistration.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.entity.RxRegistrationChargeCategory;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.entity.RxRegistrationChargeCategoryDetail;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.entity.RxRegistrationPlanChargeCategorySetting;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.entity.RxRegistrationPlanSetting;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.entity.req.RxRegistrationChargeCategoryReq;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.entity.resp.RxRegistrationChargeCategoryResp;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.mapper.RxRegistrationChargeCategoryDetailMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.mapper.RxRegistrationChargeCategoryMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.mapper.RxRegistrationPlanChargeCategorySettingMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.mapper.RxRegistrationPlanSettingMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.service.RxRegistrationChargeCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.rxRegistration.constant.ChargeCategoryTypeConstant.*;

/**
 * <p>
 * 收费项主项 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-05-07
 */
@Service
public class RxRegistrationChargeCategoryServiceImpl extends ServiceImpl<RxRegistrationChargeCategoryMapper, RxRegistrationChargeCategory> implements RxRegistrationChargeCategoryService {
    @Resource
    private RxRegistrationChargeCategoryMapper rxRegistrationChargeCategoryMapper;
    @Resource
    private RxRegistrationChargeCategoryDetailMapper rxRegistrationChargeCategoryDetailMapper;
    @Resource
    private RxRegistrationPlanChargeCategorySettingMapper rxRegistrationPlanChargeCategorySettingMapper;
    @Resource
    private RxRegistrationPlanSettingMapper rxRegistrationPlanSettingMapper;

    @Override
    public ResponseResult addRxRegistrationChargeCategory(RxRegistrationChargeCategoryReq chargeCategoryReq) {
        if (isNullOrEmpty(chargeCategoryReq.getChargeCategoryName(), chargeCategoryReq.getChargeCategoryType(),
                chargeCategoryReq.getIsRequired())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        if (chargeCategoryReq.getChargeCategoryType() != CHARGE_CATEGORY_TYPE_NORMAL
                && chargeCategoryReq.getChargeCategoryType() != CHARGE_CATEGORY_TYPE_MAJOR
                && chargeCategoryReq.getChargeCategoryType() != CHARGE_CATEGORY_TYPE_DORMITORY
                && chargeCategoryReq.getChargeCategoryType() != CHARGE_CATEGORY_TYPE_SIGNAL
                && chargeCategoryReq.getChargeCategoryType() != CHARGE_CATEGORY_TYPE_MULTIPLE) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        //普通收费项验证
        if (chargeCategoryReq.getChargeCategoryType() == CHARGE_CATEGORY_TYPE_NORMAL) {
            if (isNullOrEmpty(chargeCategoryReq.getChargeCategoryPrice())) {
                return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
            }
            if (chargeCategoryReq.getChargeCategoryPrice().compareTo(BigDecimal.ZERO) < 0) {
                return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
            }
            if (!chargeCategoryReq.getIsRequired()) {
                return CommonResult.failed(CommonCodeEnum.RX_REGISTRATION_CHARGE_CATEGORY_REQUIRED_ERROR);
            }
        }

        //专业收费项验证
        if (chargeCategoryReq.getChargeCategoryType() == CHARGE_CATEGORY_TYPE_MAJOR) {
            if (!chargeCategoryReq.getIsRequired()) {
                return CommonResult.failed(CommonCodeEnum.RX_REGISTRATION_CHARGE_CATEGORY_REQUIRED_ERROR);
            }
        }

        RxRegistrationChargeCategory chargeCategory = new RxRegistrationChargeCategory();
        chargeCategory.setChargeCategoryName(chargeCategoryReq.getChargeCategoryName());
        chargeCategory.setChargeCategoryType(chargeCategoryReq.getChargeCategoryType());
        chargeCategory.setChargeCategoryPrice(chargeCategoryReq.getChargeCategoryPrice());
        chargeCategory.setIsRequired(chargeCategoryReq.getIsRequired());
        rxRegistrationChargeCategoryMapper.insert(chargeCategory);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getRxRegistrationChargeCategoryList(String searchStr, Integer type, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<RxRegistrationChargeCategory> lambdaQuery = new LambdaQueryWrapper<>();
        if (!isNullOrEmpty(searchStr)) {
            lambdaQuery.like(RxRegistrationChargeCategory::getChargeCategoryName, searchStr);
        }
        if (!isNullOrEmpty(type)) {
            lambdaQuery.eq(RxRegistrationChargeCategory::getChargeCategoryType, type);
        }
        Page<RxRegistrationChargeCategory> page = new Page<>(pageNum, pageSize);
        rxRegistrationChargeCategoryMapper.selectPage(page, lambdaQuery);
        List<RxRegistrationChargeCategoryResp> list = page.getRecords()
                .stream()
                .map(chargeCategory -> {
                    RxRegistrationChargeCategoryResp chargeCategoryResp = new RxRegistrationChargeCategoryResp();
                    BeanUtils.copyProperties(chargeCategory, chargeCategoryResp);
                    return chargeCategoryResp;
                })
                .collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(list, total);
    }

    @Override
    public ResponseResult updateRxRegistrationChargeCategory(RxRegistrationChargeCategoryReq chargeCategoryReq) {
        if (isNullOrEmpty(chargeCategoryReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        //专业收费项验证
        if (!isNullOrEmpty(chargeCategoryReq.getChargeCategoryType())
                && !isNullOrEmpty(chargeCategoryReq.getIsRequired())
                && (chargeCategoryReq.getChargeCategoryType() == CHARGE_CATEGORY_TYPE_MAJOR
                || chargeCategoryReq.getChargeCategoryType() == CHARGE_CATEGORY_TYPE_NORMAL)) {
            if (!chargeCategoryReq.getIsRequired()) {
                return CommonResult.failed(CommonCodeEnum.RX_REGISTRATION_CHARGE_CATEGORY_REQUIRED_ERROR);
            }
        }

        RxRegistrationChargeCategory chargeCategory = rxRegistrationChargeCategoryMapper.selectById(chargeCategoryReq.getId());
        if (chargeCategory == null) {
            return CommonResult.failed(CommonCodeEnum.RX_REGISTRATION_CHARGE_CATEGORY_NOT_EXIST);
        }

        RxRegistrationChargeCategory updateChargeCategory = new RxRegistrationChargeCategory();
        updateChargeCategory.setId(chargeCategoryReq.getId());
        if (!isNullOrEmpty(chargeCategoryReq.getChargeCategoryPrice())
                && chargeCategoryReq.getChargeCategoryPrice().compareTo(BigDecimal.ZERO) > 0
                && chargeCategory.getChargeCategoryType().equals(CHARGE_CATEGORY_TYPE_NORMAL)) {
            updateChargeCategory.setChargeCategoryPrice(chargeCategoryReq.getChargeCategoryPrice());
        }
        updateChargeCategory.setChargeCategoryName(isNullOrEmpty(chargeCategoryReq.getChargeCategoryName()) ? null : chargeCategoryReq.getChargeCategoryName());
        if (!isNullOrEmpty(chargeCategoryReq.getIsRequired())){
            updateChargeCategory.setIsRequired(chargeCategoryReq.getIsRequired());
        }
        rxRegistrationChargeCategoryMapper.updateById(updateChargeCategory);
        return CommonResult.success();
    }

    @Override
    @Transactional
    public ResponseResult deleteRxRegistrationChargeCategory(RxRegistrationChargeCategoryReq chargeCategoryReq) {
        if (isNullOrEmpty(chargeCategoryReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        RxRegistrationChargeCategory chargeCategory = rxRegistrationChargeCategoryMapper.selectById(chargeCategoryReq.getId());
        if (chargeCategory == null) {
            return CommonResult.failed(CommonCodeEnum.RX_REGISTRATION_CHARGE_CATEGORY_NOT_EXIST);
        }

        //删除子收费项
        if (chargeCategory.getChargeCategoryType() == CHARGE_CATEGORY_TYPE_MAJOR) {
            rxRegistrationChargeCategoryDetailMapper.delete(new LambdaQueryWrapper<RxRegistrationChargeCategoryDetail>().eq(RxRegistrationChargeCategoryDetail::getParentId, chargeCategoryReq.getId()));
        }

        //删除主收费项
        rxRegistrationChargeCategoryMapper.delete(new LambdaQueryWrapper<RxRegistrationChargeCategory>().eq(RxRegistrationChargeCategory::getId, chargeCategoryReq.getId()));
        return CommonResult.success();
    }

    @Override
    public ResponseResult checkRxRegistrationChargeCategoryIsPlan(String chargeCategoryId) {
        RxRegistrationChargeCategory chargeCategory = rxRegistrationChargeCategoryMapper.selectById(chargeCategoryId);
        if (chargeCategory == null){
            return CommonResult.failed(CommonCodeEnum.RX_REGISTRATION_CHARGE_CATEGORY_NOT_EXIST);
        }

        List<String> planNameList = new ArrayList<>();
        List<RxRegistrationPlanChargeCategorySetting> chargeCategorySettingList = rxRegistrationPlanChargeCategorySettingMapper.selectList(new LambdaQueryWrapper<RxRegistrationPlanChargeCategorySetting>()
                .eq(RxRegistrationPlanChargeCategorySetting::getChargeCategoryId, chargeCategoryId)
                .groupBy(RxRegistrationPlanChargeCategorySetting::getPlanId));
        if (chargeCategorySettingList != null && chargeCategorySettingList.size() > 0){
            for (RxRegistrationPlanChargeCategorySetting setting:chargeCategorySettingList) {
                RxRegistrationPlanSetting planSetting = rxRegistrationPlanSettingMapper.selectById(setting.getPlanId());
                if (planSetting != null){
                    planNameList.add(planSetting.getPlanName());
                }
            }
        }
        return CommonResult.success(planNameList);
    }

}
